<template>
  <el-dialog
    title="添加合同"
    width="580px"
    :visible="showDialog"
    @close="close"
  >
    <!-- 表单区域 -->
    <div class="form-container">
      <el-form ref="addForm" :model="rentForm" :rules="rentRules" label-position="top">
        <el-form-item label="租赁楼宇" prop="buildingId">
          <el-select v-model="rentForm.buildingId" placeholder="请选择">
            <el-option
              v-for="item in rentBuildList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="租赁起止日期" prop="rentTime">
          <el-date-picker
            v-model="rentForm.rentTime"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
        <el-form-item label="租赁合同" prop="contractId">
          <el-upload
            ref="upload"
            action="#"
            :before-upload="beforeUpload"
            :http-request="uploadHandle"
          >
            <el-button size="small" type="primary" plain>上传合同文件</el-button>
            <div slot="tip" class="el-upload__tip">支持扩展名：.doc .docx .pdf, 文件大小不超过5M</div>
          </el-upload>
        </el-form-item>
      </el-form>
    </div>
    <template #footer>
      <el-button size="mini" @click="close">取 消</el-button>
      <el-button size="mini" type="primary" @click="doSubmit">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { createRentAPI, getRentBuildListAPI, uploadAPI } from '@/api/enterprise'

export default {
  name: 'RentDialog',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    enterpriseId: {
      // type: Number,
      // default: 0
    }
  },
  // props: ['enterpriseId', 'showDialog'],
  data() {
    return {
      rentForm: {
        buildingId: null, // 楼宇id
        contractId: null, // 合同id
        contractUrl: '', // 合同Url
        enterpriseId: 0, // 企业名称
        type: 0, // 合同类型
        rentTime: [] // 合同时间
      },
      rentRules: {
        buildingId: [
          { required: true, message: '请选择楼宇', trigger: 'change' }
        ],
        rentTime: [
          { required: true, message: '请选择租赁日期', trigger: 'change' }
        ],
        contractId: [
          { required: true, message: '请上传合同文件' }
        ]
      },
      rentBuildList: []
    }
  },
  mounted() {
    this.getRentBuildList()
    if (this.showDialog) {
      this.rentForm.enterpriseId = this.enterpriseId
    }
  },
  methods: {
    // 关闭弹窗
    close() {
      this.$refs.addForm.resetFields()
      this.$refs.upload.clearFiles()
      this.$emit('update:showDialog', false)
    },
    // 获取楼宇列表
    async getRentBuildList() {
      const res = await getRentBuildListAPI()
      this.rentBuildList = res.data
    },
    // 上传合同文件
    beforeUpload(file) {
      // const supportedTypes = [
      //   'image/jpeg', // .jpg 或 .jpeg
      //   'application/x-rar-compressed', // .rar
      //   'application/zip', // .zip
      //   'application/msword', // .doc
      //   'application/vnd.openxmlformats-officedocument.wordprocessingml.document', // .docx
      //   'application/pdf' // .pdf
      // ]
      // const isLt5M = file.size / 1024 / 1024 < 5
      // // 检查文件类型是否在支持的列表中
      // const isSupportedType = (file) => {
      //   return supportedTypes.includes(file.type)
      // }
      // console.log(isSupportedType(file))

      // if (!isSupportedType(file)) {
      //   this.$message.error('仅支持上传 JPG, RAR, ZIP, DOC, DOCX, PDF 格式的文件!')
      // }
      // if (!isLt5M) {
      //   this.$message.error('上传文件大小不能超过 5MB!')
      // }
      // return isSupportedType(file) && isLt5M
    },
    async uploadHandle(fileData) {
      // 1. 处理FormData
      const { file } = fileData
      const formData = new FormData()
      formData.append('file', file)
      formData.append('type', 'contract')
      // 2. 上传并赋值
      const res = await uploadAPI(formData)
      const { id, url } = res.data
      this.rentForm.contractId = id
      this.rentForm.contractUrl = url
      // 单独校验表单
      this.$refs.addForm.validate('contractId')
    },
    // 提交表单
    async doSubmit() {
      await this.$refs.addForm.validate()
      const { buildingId, contractId, contractUrl, type } = this.rentForm
      const data = { buildingId, contractId, contractUrl, type,
        enterpriseId: this.enterpriseId,
        startTime: this.rentForm.rentTime[0],
        endTime: this.rentForm.rentTime[1]
      }
      await createRentAPI(data)
      this.$emit('updateSuccess')
      this.$message.success('添加合同成功')
      this.close()
    }
  }
}
</script>

<style scoped lang='scss'>
.el-date-editor {
    width: 80%;
}

</style>
